﻿using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;

namespace Babylon.Toolbox.ModelBuilding
{
    public class BasicEffectMaterialConverter : IMaterialConverter
    {
        readonly Dictionary<MaterialContent, BasicEffect> effects = new Dictionary<MaterialContent, BasicEffect>();

        public ModelEffect Convert(GraphicsDevice device, MaterialContent material)
        {
            BasicEffect effect;
        
            if(!effects.TryGetValue(material, out effect))
            {
                effect = new BasicEffect(device);

                effect.Alpha = material.Alpha;
               
                effect.AmbientColor = material.AmbientColor;
            
                if (material.BumpTexture!=null)
                    effect.BumpTexture = ContentManager.LoadTexture2D(device, material.BumpTexture, true);
                
                effect.DiffuseColor = material.DiffuseColor;
                
                if (material.DiffuseTexture != null)
                    effect.DiffuseTexture = ContentManager.LoadTexture2D(device, material.DiffuseTexture, true);
                
                effect.EmissiveColor = material.EmissiveColor;
                
                effect.Name = material.Name;
                
                effect.SpecularColor = material.SpecularColor;
                
                if(material.SpecularPower>0)
                    effect.SpecularPower = material.SpecularPower;
                
                if (material.SpecularTexture != null)
                    effect.SpecularTexture = ContentManager.LoadTexture2D(device, material.SpecularTexture, true);
            }
            return effect;
        }
    }
}
